package rcs34.android.Namedays;

import rcs34.android.Namedays.Activity.A_Preferences;
import rcs34.android.libs.HSVOPicker.HSVOPicker;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

public class PreferenceColor extends DialogPreference {
  public static final String DEFAULT = "0;1;0.5;1";
  private String value = "";
  private View view = null;
  private Context context;
  private HSVOPicker hsvo;

  public PreferenceColor(Context ctxt, AttributeSet attrs) {
    super(ctxt, attrs);
    context = ctxt;
    this.setPositiveButtonText(R.string.OK);
    setNegativeButtonText(R.string.Cancel);
  }
  
  public void setColor(String _value){
	  value=_value;
  }

  @Override
  protected View onCreateDialogView() {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.preference_color, null);
    hsvo = (HSVOPicker) view.findViewById(R.id.hsvoPicker);
    hsvo.setOpacityEnabled(A_Preferences.prefs.getBoolean(
        GApp.context.getString(R.string.prefUseTransparency), true));
    hsvo.setStatus(value);
    return (view);
  }

  @Override
  protected void onBindDialogView(View v) {
    super.onBindDialogView(v);
  }

  @Override
  protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if (positiveResult) {
      value = hsvo.getStatus();
      if (callChangeListener(value)) {
        persistString(value);
      }
    }
  }

  @Override
  protected Object onGetDefaultValue(TypedArray _a, int _index) {
    // System.out.println("onGetDefaultValue"+_a.getString(_index));
    String result = _a.getString(_index);
    if (result != null) {
      return result;
    } else {
      return DEFAULT;
    }
  }

  @Override
  protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    // System.out.println("onSetInitialValue"+defaultValue);
    value = (restoreValue ? getPersistedString(value) : (String) defaultValue);
    if (shouldPersist()) {
      persistString(value);
    }
  }
}
